package com.lx.ngsl.exam.hsitory.service;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.lx.ngsl.exam.autopaper.entity.PaperEntity;
import com.lx.ngsl.exam.autopaper.repository.AutoPaperRepository;
import com.lx.ngsl.exam.hsitory.entity.historyEnetity;
import com.lx.ngsl.exam.hsitory.repository.HistoryViewRepository;
import com.lx.ngsl.exam.hsitory.repository.HsitoryRepository;
import com.lx.ngsl.exam.score.entity.ScoreEntity;
@Service
public class historyService {
	@Autowired 
	HsitoryRepository repository;
	@Autowired
	AutoPaperRepository  autoPaperRepository;
	@Autowired
	HistoryViewRepository historyViewRepository;
	/**
	 * 学员成绩管理
	 * @param name
	 * @param title
	 * @param pageable
	 * @return
	 */
public Page<historyEnetity> query(String name,String title,Pageable pageable) {
	PaperEntity paperEntity=autoPaperRepository.findByTitle(title);
	Page<historyEnetity> list = historyViewRepository.findAll(new Specification<historyEnetity>() {
		@Override
		public Predicate toPredicate(Root<historyEnetity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			Predicate predicate = cb.conjunction();
			List<Expression<Boolean>> expressions = predicate.getExpressions();
			if (!StringUtils.isEmpty(name)) {
				expressions.add(cb.like(root.<String> get("username"), "%" +name+"%"));
			}
			if (!StringUtils.isEmpty(title)) {
				expressions.add(cb.like(root.<String> get("title"), "%"+title+"%"));
			}
			return predicate;
		}
	}, pageable);

	return list;
}
/**
 * app端接口
 * @param userid
 * @return
 */
public List<ScoreEntity> historyquery(String userid) {
	List<ScoreEntity> scoreEntity=repository.findByUserId(userid);
//	JSONArray Json = new JSONArray();
//	Json.add(scoreEntity);
	return scoreEntity;
}
}
